/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package AplManager.controller;
import AplManager.views.*;
import AplManager.models.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
 *
 * @author user
 */
public class TableDisplayController{
     private TableDisplay view;
     private Team model;
    private ActionListener actionListener;
    private DBcontroller dbcontrol;
    public TableDisplayController(Team model,TableDisplay view,DBcontroller dbcontrol){
       this.model = model;
        this.view  = view;
        this.dbcontrol = dbcontrol;
         view.setLabel("Team: " + model.getTeamName()); 
    }
    
    public void control(){        
        actionListener = new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) { 
                  if(e.getSource() == view.getAddPlayerButton()){
                  AddPlayer addplayer = new AddPlayer();
                 addplayer.setVisible(true);
                   AddPlayerController control = new AddPlayerController(model,addplayer,view,dbcontrol);
                  control.control();
                  }
                  else if(e.getSource() == view.getEditPlayerButton()){
                   int num = view.getSelectedRowNUM();
                   EditPlayer editPlayer = new EditPlayer();
                   editPlayer.setVisible(true);
                         for(int i= 0;i < view.getNumOfRows(); i++){
                     if(view.getNumFromSelectedRow() == model.getPlayers()[i].getNumber()){
                         editPlayer.setFirstName(model.getPlayers()[i].getFirstName());
                         editPlayer.setLastName(model.getPlayers()[i].getlastName());
                         editPlayer.setNumber(model.getPlayers()[i].getNumber());
                         editPlayer.setPosition(model.getPlayers()[i].getPosition());
                         editPlayer.setGoals(model.getPlayers()[i].getGoals());
                         editPlayer.setAssists(model.getPlayers()[i].getAssists());
                 }
                 }
               EditPlayerController control = new EditPlayerController(model,editPlayer,view, num);
                      view.deleteRow();
                
                 
                      control.control();
                 
                   //AddPlayerController control = new AddPlayerController(null,addplayer);
                   //control.control();
                  }
                  else if(e.getSource() == view.getStartLineUpButton()){
                       System.out.println("I was just called");
                 StartingLineUp lineUp = new StartingLineUp();
                 int[] lineUpList = view.getLineUpList();
                    int j=0;
                 for(int i= 0;i < model.getCurSize(); i++){
                     if(lineUpList[j] == model.getPlayers()[i].getNumber()){
                     model.getPlayers()[i].trutify();
                     System.out.println(model.getPlayers()[i].isInStartingLineUp());
                     if(j!=lineUpList.length-1)
                     j++;
                 }
                     else
                      model.getPlayers()[i].falsify();
                 }
                 lineUp.addElements(model.createSrtLineUp());
                 
                 lineUp.setVisible(true);
                 StartingLineUpController control = new StartingLineUpController(null,lineUp);
                 control.control();
                   //AddPlayerController control = new AddPlayerController(null,addplayer);
                   //control.control();
                  }
                    else if(e.getSource() == view.getDeletePlayerButton()){
                      model.deletePlayer(view.getNumFromSelectedRow());
                      view.deleteRow();
                   //AddPlayerController control = new AddPlayerController(null,addplayer);
                   //control.control();
                  }
                  else if(e.getSource() == view.getExportComamand()){
                     FileIOController control = new FileIOController(model);
                     control.control();
                  }
                     else if(e.getSource() == view.getMenuBtn()){
                      dbcontrol.deleteRecords(model.getTeamID());
                      dbcontrol.addRecords();
                      view.setVisible(false);
                      Menu menu = new Menu();
                      menu.setVisible(true);
                      MenuController control = new MenuController(menu);
                        control.control();
                   //AddPlayerController control = new AddPlayerController(null,addplayer);
                   //control.control();
                  }
                  
              }
        };                
        view.getAddPlayerButton().addActionListener(actionListener);
        view.getEditPlayerButton().addActionListener(actionListener);
         view.getDeletePlayerButton().addActionListener(actionListener);
        view.getStartLineUpButton().addActionListener(actionListener);
        view.getExportComamand().addActionListener(actionListener);
        view.getMenuBtn().addActionListener(actionListener);              
    }
}

